#pragma once

#include "messiah/mcommon.h"
#include "iworld_level.h"
#include "iworld_common.h"

#include "external/tinyxml/tinyxml2.h"

#include <string>
#include <vector>
#include <map>

namespace renderit
{
	namespace messiah
	{
		class iworld_loader
		{
		public:
			iworld_loader(const std::string& fpath)
				: fpath_(fpath)
				, is_dirty_(false)
			{

			}

		public:
			bool load();
			bool save() 
			{
				if (!is_dirty_)
				{
					return true;
				}
				is_dirty_ = true;
				return save_as(fpath_); 
			}
			bool save_as(const std::string& fpath);

			std::string get_world_name(std::string fpath);
			uint32 replace_guid(const std::string& from_guid, const std::string& to_guid);

		private:
			void ifind_guid_in_path(tinyxml2::XMLElement * root_elem, const std::string& path);

		public:
			std::string fpath_;
			std::string name_;
			std::string fname_;

			tinyxml2::XMLDocument doc_;
			tinyxml2::XMLElement * GlobalEnvVolumeComponent_;

			std::vector<iworld_level> levels_;

		private:
			// { For Debug
				std::map<path_info, uint32> comp_path_;
				std::map<path_info, uint32> guid_path_;
			// }

			// guid -> guid_info
			std::map<std::string, std::vector<guid_info>> xml_guid_map_;

			bool is_dirty_;
		};
	}
}